package mx.com.matersys.vacs.service.usuarios;

import mx.com.matersys.vacs.domain.UserDetailsImpl;
import mx.com.matersys.vacs.domain.UserVacs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private UserAssembler assembler;
	
	@Override
	public UserDetails loadUserByUsername(String username) 
			throws UsernameNotFoundException, DataAccessException {
		
		UserVacs userv;
		UserDetailsImpl userImpl;
		
		System.out.println("loadUserByUsername");
		userv = UserVacs.findUserVacs(username);
		
		if (userv == null) {
			System.out.println("Usuario no valido "+ username);
			throw new UsernameNotFoundException("Usuario no valido "+ username);
		}
		
		userImpl = assembler.buildUserFromUserVacs(userv);
		
		System.out.println("User details: "+  userImpl);
		
		return userImpl;
	}

}
